package com.ln.testdrag;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ln.testdrag.draggable.SpringBackDraggable;
import com.ln.testdrag.floatwindow.MyWindowManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        findViewById(R.id.drag).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpringBackDraggable springBackDraggable = new SpringBackDraggable(SpringBackDraggable.ORIENTATION_HORIZONTAL);
                springBackDraggable.setAllowMoveToScreenNotch(false);

                FloatWindow.with(MainActivity.this)
                        .setContentView(R.layout.window_hint)
                        .setAnimStyle(R.style.IOSAnimStyle)
                        .setImageDrawable(android.R.id.icon, R.drawable.ic_launcher_background)
                        .setText(android.R.id.message, "点我消失")
                        // 设置成可拖拽的
//                .setDraggable(new MovingDraggable())
                        // 设置指定的拖拽规则
                        .setDraggable(springBackDraggable)
                        .setOnClickListener(android.R.id.message, new FloatWindow.OnClickListener<TextView>() {

                            @Override
                            public void onClick(FloatWindow<?> floatWindow, TextView view) {
                                floatWindow.cancel();
                            }
                        })
                        .show();
            }
        });
        checkFloatPermission();
    }


    private void checkFloatPermission() {
//        showConfirmDialog(this,"11111");
        if (!MyWindowManager.isWindowShowing()) {
            MyWindowManager.createSmallWindow(MainActivity.this);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void showConfirmDialog(Context context, String message) {
        Dialog dialog = new AlertDialog.Builder(context).setCancelable(true).setTitle("")
                .setMessage(message)
                .setPositiveButton("现在去开启",
                        (dialog12, which) -> {
                            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setData(Uri.parse("package:" + context.getPackageName()));
                            context.startActivity(intent);
                            dialog12.dismiss();
                        }).setNegativeButton("暂不开启",
                        (dialog1, which) -> dialog1.dismiss()).create();
        dialog.show();
    }
}